package outputstream;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileCopy {
    public static void main(String[] s) {

    }

    @Test
    public void copy() {
        /**
         * 目的把D:\sucai下的copy.png图片文件传输到D:\xuexi下
         * 读取图片的二进制数字并储存在数组中
         * 把数组通过输出流输出
         */
        //目标图片路径
        String beforePath = "D:\\sucai\\copy.png";
        //目的路径
        String afterPath = "D:\\xuexi\\shadow.png";
        //读取文件对象
        FileInputStream fileInputStream = null;
        //输出文件对象
        FileOutputStream fileOutputStream = null;
        byte[] bytes = new byte[1024];
        int readLen = 0;
        int much=0;
        try {
            //读取文件对象
            fileInputStream = new FileInputStream(beforePath);
            //输出文件对象
            fileOutputStream = new FileOutputStream(afterPath,true);
            //fileInputStream.read(bytes)将图片的字节流读入入bytes数组
            while ((readLen = fileInputStream.read(bytes)) != -1) {
            //将数组中的字节写入目标文件
                //边读边写
                fileOutputStream.write(bytes,0,readLen);//一定要用方法参数规定读取数组长度，以防乱码
                much++;
            }
            System.out.println("拷贝成功");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fileInputStream.close();
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
